﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WR.BL.BE;
using SAP.Middleware.Connector;

namespace WR.DL.DALC.SAP
{
    public class MaterialDALC
    {
        private string _connId = string.Empty;

        #region Singleton
        public static MaterialDALC getInstance(string pConnId)
        {
            if (_instance == null)
            {
                lock (typeof(MaterialDALC))
                {
                    if (_instance == null)
                    {
                        _instance = new MaterialDALC(pConnId);
                    }
                }
            }
            return _instance;
        }
        private MaterialDALC(string pConnId) { this._connId = pConnId; }
        private static volatile MaterialDALC _instance = null;
        #endregion


        public MaterialBE read(string pTienda, string pCod)
        {
            MaterialBE objMat = null; IRfcFunction function = null;
            InMemoryDestinationConfiguration dc = new InMemoryDestinationConfiguration();
            try
            {
                //RfcConfigParameters cp = new RfcConfigParameters(); //GenericDALC.getConfigParam(pObjConn);
                //cp.Add(RfcConfigParameters.Name, this._connId);
                //RfcDestination dest = RfcDestinationManager.GetDestination(cp);
                try
                {
                    RfcDestinationManager.RegisterDestinationConfiguration(dc);
                }
                catch { }
                RfcDestination dest = RfcDestinationManager.GetDestination(this._connId);

                function = dest.Repository.CreateFunction("ZWR_SD_001");
                function.SetValue("I_WERKS", pTienda);
                function.SetValue("I_MATNR", pCod);
                function.Invoke(dest);

                IRfcTable T_MARA = function.GetTable("T_MARA");
                foreach (IRfcStructure s_mara in T_MARA)
                {
                    objMat = new MaterialBE();
                    objMat.Codigo = s_mara["MATNR"].GetString();
                    objMat.Descripcion = s_mara["MAKTX"].GetString();
                    
                    objMat.CodTienda = s_mara["WERKS"].GetString();
                    objMat.Empresa = s_mara["NAME1"].GetString();
                    objMat.NomTienda = s_mara["NAME2"].GetString();

                    objMat.Ean = s_mara["EAN11"].GetString();
                    objMat.GrpArt = s_mara["MATKL"].GetString();
                    objMat.Jerarquia = s_mara["PRDHA"].GetString();

                    objMat.PesoBruto = s_mara["BRGEW"].GetDecimal();
                    objMat.PesoNeto = s_mara["NTGEW"].GetDecimal();
                    objMat.Precio = s_mara["KBETR"].GetDecimal();
                    objMat.Stock = s_mara["EISBE"].GetDecimal();

                    objMat.Ramo = s_mara["MBRSH"].GetString();
                    objMat.TipoMat = s_mara["MTART"].GetString();
                    objMat.UMedida = s_mara["MEINS"].GetString();
                    objMat.UMPeso = s_mara["GEWEI"].GetString();
                }
            }
            catch (RfcCommunicationException e)
            {
                // network problem...
            }
            catch (RfcLogonException e)
            {
                // user could not logon...
            }
            catch (RfcAbapRuntimeException e)
            {
                // serious problem on ABAP system side...
            }
            catch (RfcAbapBaseException e)
            {
                // The function module returned an ABAP exception, an ABAP message
                // or an ABAP class-based exception...
            }
            catch (RfcBaseException e)
            {
                // Grabar Log de errores con e.message
                throw new Exception(e.Message);
                return null;
            }
            finally
            {
                RfcDestinationManager.UnregisterDestinationConfiguration(dc);
            }
            return objMat;
        }

        public List<MaterialBE> find(string pTienda, string pCod, string pDesc)
        {
            //Máximo de registros
            int count = 20;
            try { count = Convert.ToInt16(DALC.getParamValue("MAXMAT")); } catch { }
            //-------------------

            MaterialBE objMat = null; List<MaterialBE> lstMat = new List<MaterialBE>();
            IRfcFunction function = null;
            InMemoryDestinationConfiguration dc = new InMemoryDestinationConfiguration();
            try
            {
                //RfcConfigParameters cp = new RfcConfigParameters(); //GenericDALC.getConfigParam(pObjConn);
                //cp.Add(RfcConfigParameters.Name, this._connId);
                //RfcDestination dest = RfcDestinationManager.GetDestination(cp);
                //List<RfcConnectionData> cd = dest.Monitor.GetConnectionsData();
                try
                {
                    RfcDestinationManager.RegisterDestinationConfiguration(dc);
                }
                catch { }
                RfcDestination dest = RfcDestinationManager.GetDestination(this._connId);
                function = dest.Repository.CreateFunction("ZWR_SD_001");
                function.SetValue("I_WERKS", pTienda);
                function.SetValue("I_NROWS", count);
                if (!string.IsNullOrEmpty(pCod))
                {
                    function.SetValue("I_MATNR", pCod);
                }
                if (!string.IsNullOrEmpty(pDesc))
                {
                    function.SetValue("I_MAKTX", pDesc);
                }
                function.Invoke(dest);

                IRfcTable T_MARA = function.GetTable("T_MARA");
                foreach (IRfcStructure s_mara in T_MARA)
                {
                    objMat = new MaterialBE();
                    objMat.Codigo = s_mara["MATNR"].GetString();
                    objMat.Descripcion = s_mara["MAKTX"].GetString();

                    objMat.CodTienda = s_mara["WERKS"].GetString();
                    objMat.Empresa = s_mara["NAME1"].GetString();
                    objMat.NomTienda = s_mara["NAME2"].GetString();

                    objMat.Ean = s_mara["EAN11"].GetString();
                    objMat.GrpArt = s_mara["MATKL"].GetString();
                    objMat.Jerarquia = s_mara["PRDHA"].GetString();

                    objMat.PesoBruto = s_mara["BRGEW"].GetDecimal();
                    objMat.PesoNeto = s_mara["NTGEW"].GetDecimal();
                    objMat.Precio = s_mara["KBETR"].GetDecimal();
                    objMat.Stock = s_mara["EISBE"].GetDecimal();

                    objMat.Ramo = s_mara["MBRSH"].GetString();
                    objMat.TipoMat = s_mara["MTART"].GetString();
                    objMat.UMedida = s_mara["MEINS"].GetString();
                    objMat.UMPeso = s_mara["GEWEI"].GetString();

                    lstMat.Add(objMat);
                }
                
            }
            catch (RfcCommunicationException e)
            {
                // network problem...
            }
            catch (RfcLogonException e)
            {
                // user could not logon...
            }
            catch (RfcAbapRuntimeException e)
            {
                // serious problem on ABAP system side...
            }
            catch (RfcAbapBaseException e)
            {
                // The function module returned an ABAP exception, an ABAP message
                // or an ABAP class-based exception...
            }
            catch (RfcBaseException e)
            {
                // Grabar Log de errores con e.message
                throw new Exception(e.Message);
            }
            finally
            {
                RfcDestinationManager.UnregisterDestinationConfiguration(dc);
            }
            return lstMat;
        }

        public List<MaterialBE> list(string pTienda)
        {
            //Máximo de registros
            int count = 20;
            try { count = Convert.ToInt16(DALC.getParamValue("MAXMAT")); }catch { }
            //-------------------

            MaterialBE objMat = null; List<MaterialBE> lstMat = new List<MaterialBE>();
            IRfcFunction function = null;
            InMemoryDestinationConfiguration dc = new InMemoryDestinationConfiguration();
            try
            {
                //RfcConfigParameters cp = new RfcConfigParameters(); //GenericDALC.getConfigParam(pObjConn);
                //cp.Add(RfcConfigParameters.Name, this._connId);
                //RfcDestination dest = RfcDestinationManager.GetDestination(cp);
                try
                {
                    RfcDestinationManager.RegisterDestinationConfiguration(dc);
                }
                catch { }
                RfcDestination dest = RfcDestinationManager.GetDestination(this._connId);
                function = dest.Repository.CreateFunction("ZWR_SD_001");
                function.SetValue("I_WERKS", pTienda);
                function.SetValue("I_NROWS", count);
                function.Invoke(dest);

                IRfcTable T_MARA = function.GetTable("T_MARA");
                foreach (IRfcStructure s_mara in T_MARA)
                {
                    objMat = new MaterialBE();
                    objMat.Codigo = s_mara["MATNR"].GetString();
                    objMat.Descripcion = s_mara["MAKTX"].GetString();

                    objMat.CodTienda = s_mara["WERKS"].GetString();
                    objMat.Empresa = s_mara["NAME1"].GetString();
                    objMat.NomTienda = s_mara["NAME2"].GetString();

                    objMat.Ean = s_mara["EAN11"].GetString();
                    objMat.GrpArt = s_mara["MATKL"].GetString();
                    objMat.Jerarquia = s_mara["PRDHA"].GetString();

                    objMat.PesoBruto = s_mara["BRGEW"].GetDecimal();
                    objMat.PesoNeto = s_mara["NTGEW"].GetDecimal();
                    objMat.Precio = s_mara["KBETR"].GetDecimal();
                    objMat.Stock = s_mara["EISBE"].GetDecimal();

                    objMat.Ramo = s_mara["MBRSH"].GetString();
                    objMat.TipoMat = s_mara["MTART"].GetString();
                    objMat.UMedida = s_mara["MEINS"].GetString();
                    objMat.UMPeso = s_mara["GEWEI"].GetString();

                    lstMat.Add(objMat);
                }
            }
            catch (RfcCommunicationException e)
            {
                // network problem...
            }
            catch (RfcLogonException e)
            {
                // user could not logon...
            }
            catch (RfcAbapRuntimeException e)
            {
                // serious problem on ABAP system side...
            }
            catch (RfcAbapBaseException e)
            {
                // The function module returned an ABAP exception, an ABAP message
                // or an ABAP class-based exception...
            }
            catch (RfcBaseException e)
            {
                // Grabar Log de errores con e.message
                throw new Exception(e.Message);
                return new List<MaterialBE>();
            }
            finally
            {
                RfcDestinationManager.UnregisterDestinationConfiguration(dc);
            }
            return lstMat;
        }
    }
}
